/*
 * Created by JFormDesigner on Mon Nov 10 08:24:34 CST 2025
 */

package com.yeban.bams.server;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import com.yeban.bams.server.log.LogManager;
import com.yeban.bams.server.log.LogEntry;

/**
 * @author yeban
 */
public class LogPanel extends JPanel {
    private DefaultTableModel model;
    private final Runnable refreshTask = this::refreshLogs;

    public LogPanel() {
        initComponents();
        initTable();
        LogManager.addListener(refreshTask);
        refreshLogs();
    }

    private void back(ActionEvent e) {
        LogManager.removeListener(refreshTask);
        Container c = SwingUtilities.getWindowAncestor(this);
        if (c instanceof ATMServer) {
            ATMServer atm = (ATMServer) c;
            CardLayout cl = (CardLayout) atm.getContentPane().getLayout();
            cl.show(atm.getContentPane(), "rankPanel");
            atm.rankPanel.startRefresh();
        }
    }

    private void allAccs(ActionEvent e) {
        // TODO add your code here
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        dataLabel = new JLabel();
        panel1 = new JPanel();
        backButton = new JButton();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();

        //======== this ========
        setLayout(null);

        //---- dataLabel ----
        dataLabel.setText("\u65e5  \u5fd7  \u4fe1  \u606f");
        dataLabel.setHorizontalAlignment(SwingConstants.CENTER);
        dataLabel.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 30));
        add(dataLabel);
        dataLabel.setBounds(115, 40, 275, 65);

        //======== panel1 ========
        {
            panel1.setLayout(null);

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel1.getComponentCount(); i++) {
                    Rectangle bounds = panel1.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel1.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel1.setMinimumSize(preferredSize);
                panel1.setPreferredSize(preferredSize);
            }
        }
        add(panel1);
        panel1.setBounds(new Rectangle(new Point(45, 140), panel1.getPreferredSize()));

        //---- backButton ----
        backButton.setText("\u8fd4\u56de");
        backButton.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 14));
        backButton.addActionListener(e -> back(e));
        add(backButton);
        backButton.setBounds(370, 550, 100, 45);

        //======== scrollPane1 ========
        {

            scrollPane1.setViewportView(table1);
        }
        add(scrollPane1);
        scrollPane1.setBounds(25, 115, 440, 420);

        setPreferredSize(new Dimension(500, 650));
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    private void initTable() {
        model = new DefaultTableModel(new Object[][]{}, new String[]{"时间","用户","操作"}){
            @Override public boolean isCellEditable(int r, int c) { return false; }
        };
        table1.setModel(model);
    }

    private void refreshLogs() {
        java.util.List<LogEntry> logs = LogManager.getLogs();
        model.setRowCount(0);
        for (LogEntry e : logs) {
            model.addRow(new Object[]{e.getTime(), e.getWho(), e.getAction()});
        }
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel dataLabel;
    private JPanel panel1;
    private JButton backButton;
    private JScrollPane scrollPane1;
    private JTable table1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
